package com.flycms.web.front.rest;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.flycms.core.base.BaseController;
import com.flycms.core.entity.PagingParam;
import com.flycms.core.entity.RestDataVo;
import com.flycms.core.utils.SnowFlake;
import com.flycms.core.utils.web.utils.CommonUtils;
import com.flycms.module.soft.model.ApplyTool;
import com.flycms.module.soft.model.SoftApply;
import com.flycms.module.soft.service.SoftApplyService;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年7月6日
 * @Date 2020年7月6日
 */

@RestController
@RequestMapping("/sa")
public class SoftApplyController extends BaseController {

    private static final Logger logger = LogManager.getLogger(SoftApplyController.class);
    
    @Autowired
    protected SoftApplyService softApplyService;
    
    @PostMapping(value = { "/apply/add" })
    public RestDataVo addApply(@Valid @RequestBody SoftApply apply, BindingResult errors) {
        RestDataVo rest = null;
        String validResult = CommonUtils.validParams(errors, logger);
        if (StringUtils.isNotEmpty(validResult)) {
            return RestDataVo.ERROR(validResult);
        }
        Long id = new SnowFlake(2, 3).nextId();
        Date now = new Date();
        apply.setId(id);
        apply.setCreateTime(now);
        apply.setUpdateTime(now);
//        reply
        try {
            softApplyService.addSoftApply(apply);
            rest = RestDataVo.OK(apply);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("申请异常，" + e.getMessage());
        }
        return rest;
    }
    
    @GetMapping(value = "/tools")
    public RestDataVo getApplyToolList(@RequestParam(value = "toolName", required = false) String toolName,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        RestDataVo rest = null;
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        try {
            List<ApplyTool> toolList = softApplyService.getApplyToolList(toolName, paging.getOffset(), paging.getRows());
            int count = softApplyService.getApplyToolCount(toolName);
            rest = RestDataVo.PAGE(toolList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取申请工具列表失败");
        }
        return rest;
    }
    
}
